package com.daftmaster.service.impl;

import com.daftmaster.entity.UserEntity;
import com.daftmaster.mapper.UserMapper;
import com.daftmaster.service.UserQueryService;
import com.daftmaster.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Daft_V
 * @Date 2022/3/20 17:21
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserQueryService userQueryService;

    @Override
    public Boolean userLogin(String user_account, String user_password) {
        UserEntity userEntity=userMapper.selectUserByAccont(user_account);
        if(userEntity==null){
            return false;
        }else{
            if(user_password.equals(userEntity.getUser_password())){
                return true;
            }else{
                return false;
            }
        }
    }

    @Override
    public JSONObject userRegister(String userName, String userAccount, String userPassword, String phone, String email) {
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> userMap=new HashMap<>();
        userMap.put("userName",userName);
        userMap.put("userAccount",userAccount);
        userMap.put("userPassword",userPassword);
        userMap.put("phone",phone);
        userMap.put("email",email);
        List<UserEntity> userEntityList=userQueryService.list();
        for (UserEntity userEntity:userEntityList
             ) {
            if(userAccount.equals(userEntity.getUser_account())){
                map.put("status",false);
                map.put("code",666);
                map.put("message","userAccount already exist");
                map.put("data",userMap);
                JSONObject jsonObject=JSONObject.fromObject(map);
                return jsonObject;
            }else if(phone.equals(userEntity.getPhone())){
                map.put("status",false);
                map.put("code",666);
                map.put("message","phone number already exist");
                map.put("data",userMap);
                JSONObject jsonObject=JSONObject.fromObject(map);
                return jsonObject;
            }
        }
        UserEntity userEntity=new UserEntity();
        userEntity.setUser_name(userName);
        userEntity.setUser_account(userAccount);
        userEntity.setUser_password(userPassword);
        userEntity.setPhone(phone);
        userEntity.setEmail(email);
        try {
            userQueryService.getBaseMapper().insert(userEntity);
            map.put("status",true);
            map.put("code",200);
            map.put("message","User register success");
            map.put("data",userMap);
            JSONObject jsonObject=JSONObject.fromObject(map);
            return jsonObject;
        }catch (Exception e){
            e.printStackTrace();
            userQueryService.getBaseMapper().insert(userEntity);
            map.put("status",false);
            map.put("code",666);
            map.put("message","User register failed");
            map.put("data",userMap);
            JSONObject jsonObject=JSONObject.fromObject(map);
            return jsonObject;
        }
    }
}
